package com.tjsinfo.tjsoa.vote.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.vote.bo.ITVoteConformationBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteEstimatedBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteEstimatedStatusBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteHeadBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteValueBO;
import com.tjsinfo.tjsoa.vote.vo.TVoteConformation;
import com.tjsinfo.tjsoa.vote.vo.TVoteEstimated;
import com.tjsinfo.tjsoa.vote.vo.TVoteEstimatedStatus;
import com.tjsinfo.tjsoa.vote.vo.TVoteHead;
import com.tjsinfo.tjsoa.vote.vo.TVoteValue;

public class HeadStatusAction extends Action {
	private ITVoteHeadBO headBo;
	private ITVoteConformationBO conformationBo;
	private ITVoteEstimatedBO estimatedBo;
	private ITVoteEstimatedStatusBO estimatedStatusBo;
	private ITVoteValueBO valueBo;
	public void setConformationBo(ITVoteConformationBO conformationBo) {
		this.conformationBo = conformationBo;
	}
	public void setValueBo(ITVoteValueBO valueBo) {
		this.valueBo = valueBo;
	}
	public void setEstimatedBo(ITVoteEstimatedBO estimatedBo) {
		this.estimatedBo = estimatedBo;
	}
	public void setEstimatedStatusBo(ITVoteEstimatedStatusBO estimatedStatusBo) {
		this.estimatedStatusBo = estimatedStatusBo;
	}
	public void setHeadBo(ITVoteHeadBO headBo) {
		this.headBo = headBo;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String status = request.getParameter("status");
			String headId = request.getParameter("headId");
			
			TVoteHead vh = new TVoteHead();
			vh = headBo.findById(headId);
			
			if("1".equals(status)){
				TVoteEstimatedStatus ves = new TVoteEstimatedStatus();
				String vote = vh.getVote();
				String voteName = vh.getVoteName();
				if(vote.substring(vote.length()-1, vote.length())==",")vote = vote.substring(0, vote.length()-1);
				if(voteName.substring(voteName.length()-1, voteName.length())==",")voteName = voteName.substring(0, voteName.length()-1);
				String str[] = vote.split(",");
				String str1[] = voteName.split(",");
				List<TVoteEstimated> estimatedLists = estimatedBo.find(headId);
				for (int i = 0; i < str.length; i++) {
					ves.setHeadId(headId);
					ves.setVote(str[i]);
					ves.setVoteName(str1[i]);
					ves.setStatus(0);
					for(TVoteEstimated estimatedList : estimatedLists){
						ves.setEstimatedId(estimatedList.getId());
						ves.setName(estimatedList.getName());
						ves.setPost(estimatedList.getPost());
						ves.setOrderId(estimatedList.getOrderId());
						estimatedStatusBo.save(ves);
					}
				}
				request.setAttribute("message", "vote.headStatus1.message");
			}else if("2".equals(status)){
				List<TVoteEstimatedStatus> estimatedStatusLists = estimatedStatusBo.findStatus(headId);
				List<TVoteConformation> conformationLists = conformationBo.find(headId);
				TVoteValue vv = new TVoteValue();
				for (TVoteEstimatedStatus estimatedStatusList : estimatedStatusLists) {
					for (TVoteConformation conformationList : conformationLists) {
                        vv.setHeadId(headId);
						vv.setConformationId(conformationList.getId());
						vv.setValueKey("0");
                        vv.setVote(estimatedStatusList.getVote());
						vv.setEstimated(estimatedStatusList.getEstimatedId());
						vv.setEstimatedName(estimatedStatusList.getName());
						valueBo.save(vv);
					}
				}
				request.setAttribute("message", "vote.headStatus2.message");
			} 
			vh.setStatus(Integer.parseInt(status));
			
			headBo.update(vh);
			
			request.setAttribute("linkurl", "tjsinfo/tjsoa/vote/headList.do");
			
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
	}
}
